
<template xmlns:el-col="http://www.w3.org/1999/html">
  <div class="app-container">
    角色管理

    <!--查询表单-->
    <div class="search-div">
    <el-form lable-width="70px" size="small">
    <el-row>
      <el-col>
      <el-form-item label="角色名称">
        <el-input style="width:20%" v-model="searchObj.roleName" placeholder="角色名称" ></el-input>
    </el-form-item>
      </el-col>
    </el-row>
    <el-row stwle="display:flex">
      <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
      </el-row>
    </el-form>
  </div>
    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
      <el-button class="bnt-add"  size="mini" @click="batchRemoveRoles()">批 量 删 除</el-button>
    </div>

    <!-- 列表 --->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{(page -1)* limit +scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column prop ="id" label="id号"/>
      <el-table-column prop ="roleName" label="角色名称"/>
      <el-table-column prop="roleCode" label="角色编码"/>
      <el-table-column prop ="description" label="描述"/>
      <el-table-column prop ="createBy" label="创建人"/>
      <el-table-column prop ="createTime" label="创建时间" width="160"/>
      <el-table-column prop ="updateBy" label="更新人"/>
      <el-table-column prop ="updateTime" label="更新时间" width="160"/>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="edits(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="removeDataById(scope.row.id)">删除</el-button>
          <el-button type="warning" icon="el-icon-baseball" size="mini" @click="showAssignAuth(scope.row)" title="分配权限">分配权限</el-button>

        </template>
      </el-table-column>
    </el-table>
<!--    &lt;!&ndash;分页组件 &ndash;&gt;
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding:30px 0"
      text-align:center
      layout = "total,prev,pager,next,jumper"
      @current-change="fetchData">
    </el-pagination>-->

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

    <!-- 新增角色 -->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysRole" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="角色编码">
          <el-input v-model="sysRole.roleCode"/>
        </el-form-item>
        <el-form-item label="角色名称">
          <el-input v-model="sysRole.roleName"/>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="sysRole.description"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button icon="el-icon-refresh-right" @click="dialogVisible = false" size="small">取 消</el-button>
      <el-button type="primary" icon="el-icon-check" @click="saveorupdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    </div>

</template>

<script lang="js">
import api from '@/api/system/role/sysRole'
export default {
  // 定义变量和初始值
  data() {
    return {
      dialogVisible: false,
      listLoading: false,
      list: [], // 数据列表
      total: 0, // 总数
      pageSizes: [5, 10, 20, 50, 100, 200],
      page: 1, // 当前页
      limit: 10, // 每页显示的条数
      searchObj: {}, //
      sysRole: {},
      multipleSelection: []  // 多选框
    }
  },
  // 页面渲染
  created() {
    this.fetchData()
  },
  // 方法调用
  methods: {

    //分配权限
    showAssignAuth(row) {
      this.$router.push('/system/sysRole/assignAuth?id='+row.id+'&roleName='+row.roleName);
    },
    //多选框
    handleSelectionChange(SelectionVals) {
      this.multipleSelection = SelectionVals
    },
    //重置
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    //删除
    removeDataById(id) {
      return this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return api.removeId(id)
      }).then(res => {
        this.$message.success(res.message)
        this.fetchData()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    fetchData(pageNum = 1) {
      this.page = pageNum // 当前页赋值
      api.getPageList(this.page, this.limit, this.searchObj)
        .then(res => {
          this.list = res.data.records
          this.total = res.data.total
          this.$message.success(res.message)
         // console.log(this.searchObj)
        })
    },
    //添加
    add() {
      this.dialogVisible = true
      this.sysRole = {}
    },
    //添加或修改
    saveorupdate() {
      if ((!this.sysRole.id)) {
        this.saveRole()
      } else {
        this.updateRole()
      }
    },
    //跟新
    updateRole() {
      api.updateRole(this.sysRole)
        .then(res => {
          this.dialogVisible = false
          this.fetchData()
          this.$message.success(res.message)
        })
    },
    //添加
    saveRole() {
      api.saveRole(this.sysRole)
        .then(res => {
          this.dialogVisible = false
          this.fetchData()
          this.$message.success(res.message)
        })
    },
    //修改
    edits(id) {

      this.dialogVisible = true
      api.getRoleById(id).then(response => {
        this.sysRole = response.data
      })
    },
    //批量删除
    batchRemoveRoles() {
      if (this.multipleSelection.length === 0){
        this.$message.warning('请选择要删除的记录')
        return
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const idList = []
        for (let i = 0; i < this.multipleSelection.length; i++) {
          const obj = this.multipleSelection[i]
          const id = obj.id
          idList.push(id)
        }
        api.batchremoveRoles(idList)
      }).then(res => {
        this.fetchData()
        this.$message.success("删除成功")

      })
    }

  }
}
</script>

<style scoped lang="scss">

</style>
